package com.hsnn.medstgmini.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class DbUtil {
	public static String resetSql(String sql) {
		String ret = "";
		String s = sql;
		s = s.replaceAll("( )(F|f)(R|r)(O|o)(M|m)( )", " FROM ");
		s = s.replaceAll("(.*)(S|s)(E|e)(L|l)(E|e)(C|c)(T|t)( )", " SELECT ");
		s = s.replaceAll("( )(G|g)(R|r)(O|o)(U|u)(P|p)( )(B|b)(Y|y)( )",
				" GROUP BY ");
		s = s.replaceAll("( )(O|o)(R|r)(D|d)(E|e)(R|r)( )(B|b)(Y|y)( )",
				" ORDER BY ");
		int indexFrom = s.indexOf("FROM");
		while (s.indexOf("FROM") >= 0) {
			indexFrom = indexFrom + 4;
			String s1 = s.substring(0, indexFrom);
			s = s.substring(indexFrom);
			int indexWhere = s1.indexOf("WHERE");
			if (indexWhere > 0) {
				indexWhere = indexWhere + 5;
				s1 = s1.substring(0, indexWhere)
						+ s1.substring(indexWhere).replace("WHERE", "AND");
			}
			ret += s1;
			indexFrom = s.indexOf("FROM");
		}
		int indexWhere = s.indexOf("WHERE");
		if (indexWhere > 0) {
			indexWhere = indexWhere + 5;
			s = s.substring(0, indexWhere)
					+ s.substring(indexWhere).replace("WHERE", "AND");
		}
		ret += s;
		return ret;
	}

	public static String filter(String value) {
		if (value == null) {
			return null;
		}
		StringBuffer result = new StringBuffer(value.length());
		for (int i = 0; i < value.length(); i++) {
			switch (value.charAt(i)) {
			case '<':
				result.append("&lt;");
				break;
			case '>':
				result.append("&gt;");
				break;
			case '"':
				result.append("&quot;");
				break;
			case '\'':
				result.append("&#39;");
				break;
			case '%':
				result.append("&#37;");
				break;
			case ';':
				result.append("&#59;");
				break;
			case '(':
				result.append("&#40;");
				break;
			case ')':
				result.append("&#41;");
				break;
			case '&':
				result.append("&amp;");
				break;
			case '+':
				result.append("&#43;");
				break;
			default:
				result.append(value.charAt(i));
				break;
			}
		}
		return result.toString();
	}

	public static List<String> resetParam(List<String> param) {
		if (param == null || param.size() == 0) {
			return null;
		}
		List<String> newParam = new ArrayList<String>();
		for (int index = 0; index < param.size(); index++) {
			String value = param.get(index);
			if (value == null || value.equals("")) {
				newParam.add(value);
				continue;
			}
			String result = filter(value);
			newParam.add(result);
		}
		return newParam;
	}

	public static Object resetObject(Object obj) {
		if (obj == null) {
			return obj;
		}
		String className = obj.getClass().getName();
		try {
			Class<?> objClass = Class.forName(className);

			Method[] methods = objClass.getDeclaredMethods();
			for (int i = 0; i < methods.length; i++) {
				Method getMethod = methods[i];
				if (getMethod.getGenericReturnType().equals(String.class)) {
					String value = (String) getMethod.invoke(obj);
					String result = filter(value);

					String setName = getMethod.getName().replaceFirst("get",
							"set");
					Method setMethod = objClass.getDeclaredMethod(setName,
							String.class);
					Object arglist[] = new Object[1];
					arglist[0] = result;
					setMethod.invoke(obj, arglist);
				}
			}
		} catch (Throwable e) {
			return obj;
		}
		return obj;
	}

	/**
	 * Controller层 将前台传来的json对象以键-值对的方式放入page.getConditions()
	 * @author yxc
	 * @param request
	 * @param page
	 */
	@SuppressWarnings("serial")
	public static void jsonToMap(HttpServletRequest request, Pagination page) {
		Enumeration<?> paramNames = request.getParameterNames();
		List<String> list = new ArrayList<String>(){{
			add("_search");
			add("nd");
			add("rows");
			add("page");
			add("sidx");
			add("sord");
		}};
		while ( paramNames.hasMoreElements()) {
			// 获取 键名
			String paramName = (String) paramNames.nextElement();
			if (isExistparams(paramName,list)) {
				String[] paramValues = request.getParameterValues(paramName);
				if (paramValues.length == 1 ) {
					String paramValue = paramValues[0].replace("+", "");
			        if (paramValues[0].length() != 0) {
						page.getConditions().put(paramName, paramValue);
			        }  
				}
			}
		}
	}
	
	private static boolean isExistparams (String paramName,List<String> list){
		// 过滤前台分页字符串即list集合
		boolean temp = true; 
		for (int i = 0; i < list.size();i++) {
			if (paramName.equals(list.get(i))) {
				list.remove(i);
				 temp = false;
			}
		}
		return temp;
	}
	
	
}
